package com.edu.controller;

import com.edu.constant.MessageConstant;
import com.edu.constant.RedisConstant;
import com.edu.entity.PageResult;
import com.edu.entity.QueryPageBean;
import com.edu.entity.Result;
import com.edu.pojo.TSetmeal;
import com.edu.service.SetmealService;
import com.edu.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.net.MulticastSocket;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Resource
    private JedisPool jedisPool;

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("spring-jobs.xml");
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.indexOf(".");
        String ex = originalFilename.substring(index - 1);

        String fileName = UUID.randomUUID().toString() + ex;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }


    @RequestMapping("/add")
    public Result add(@RequestBody TSetmeal tSetmeal,Integer[] checkgroupIds){
        try{
            setmealService.add(tSetmeal,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.pageQuery(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        try {
            List<TSetmeal> list = setmealService.findAll();
            return new Result(true,MessageConstant.GET_SETMEAL_LIST_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_LIST_FAIL);
        }

    }


    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            TSetmeal tSetmeal = setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,tSetmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
